package com.seednia.module.core.xport;

import java.io.OutputStream;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 * 
 * @author Young Suk Ahn
 *
 */
public class ExporterXml extends AbstractExporter  {

	public static final String FORMAT = "xml";
	
	public ExporterXml()
	{
		super.setFormat(FORMAT);
	}


	/**
	 * Jaxb Marshalling
	 * @param objToExport
	 * @param os
	 * @throws JAXBException
	 */
	public void exportTo(XportDataContainer objToExport, OutputStream os, Map<String, Object> params) throws Exception
	{
		// === Marshaling ===
		// Create a JAXBContext for the LogtInfo class
		//JAXBContext jaxbCtx = JAXBContext.newInstance(objToExport.getClass());
		JAXBContext jaxbCtx = JAXBContext.newInstance(getXportingTypes());
		
		// Generate an XML schema from the annotated object model; create it
		// in the dir specified earlier under the default name, schema1.xsd
		/*
		jaxbCtx.generateSchema(new SchemaOutputResolver() {
			   @Override
			   public Result createOutput(String namespaceUri, String schemaName) throws IOException {
					return new StreamResult(new File(dir, schemaName));
			   }
		   });
		 */

		// Create a marshaler   
		Marshaller marshaller = jaxbCtx.createMarshaller();
		// The JAXB_FORMATTED_OUTPUT formats output with linefeeds and indentation
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		// Marshal the data in the bean object to the xml file
		marshaller.marshal(objToExport, os);
	}


}
